package com.education.realtime.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 时间格式转换
 * @Author lzc
 * @Date 2022/5/20 10:07
 */
public class TimeUtil {
    
    public static String toDate(Long ts, String... format) {
        String f = "yyyy-MM-dd";
        if (format.length >= 1) {
            f = format[0];
        }
        return new SimpleDateFormat(f).format(ts);
    }
    
    public static <T> List<T> toList(Iterable<T> elements) {
        ArrayList<T> list = new ArrayList<>();
        elements.forEach(list::add);
        return list;
    }

    public static String toDateTime(long ts, String... format) {
        String f = "yyyy-MM-dd HH:mm:ss";
        if (format.length >= 1) {
            f = format[0];
        }
        return new SimpleDateFormat(f).format(ts);

    }

    public static long toTs(String date, String f) throws ParseException {

        return new SimpleDateFormat(f).parse(date).getTime();
    }

    public static boolean compareLTZ(String currentTime, String lastTime) {

        //时间格式： 2022-05-23 06:12:06.768Z  2022-05-23 06:12:06.8Z
        String[] one = currentTime.replaceAll("Z", "").split("\\.");
        String[] two = lastTime.replaceAll("Z", "").split("\\.");

        String oneDateTime = one[0];
        String twoDateTime = two[0];

        int oneMs = Integer.parseInt((one[1] + "000").substring(0, 3));
        int twoMs = Integer.parseInt((two[1] + "000").substring(0, 3));

        //先判断年月日
        int result = oneDateTime.compareTo(twoDateTime);

        if (result == 0){
            result = oneMs - twoMs;
        }
        return result >= 0;

    }
}

